//
//  AY_BookShelEditPage.m
//  AiYue
//
//  Created by boWen on 2023/11/15.
//

#import "AY_BookShelEditPage.h"
#import "AY_BookShelEditCell.h"
@interface AY_BookShelEditPage ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong,nonatomic) UICollectionView *collectionView;
@property (nonatomic, assign) BOOL isAllSel;//是否全选
@property (nonatomic, strong) UIButton * quanXBtn;
@property (nonatomic, strong) NSArray<NSIndexPath *> *seleAry;//所选ary
@property (nonatomic, strong) UIButton* sureBtn;
@end

@implementation AY_BookShelEditPage

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initWithView];
}

- (void)initWithView {
    [self setupNavView];
    [self.view addSubview:self.collectionView];
    [self setupBootView];
}


#pragma mark -- UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookShelEditCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AY_BookShelEditCell" forIndexPath:indexPath];
    BookSelfModel *model = [[BookSelfModel alloc] init];
    model = self.dataSource[indexPath.row];
    [cell sendBookSelfModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.seleAry = [self.collectionView indexPathsForSelectedItems];
    [self.sureBtn setTitle:[NSString stringWithFormat:@"确认删除(%ld)",self.seleAry.count] forState:UIControlStateNormal];
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.seleAry = [self.collectionView indexPathsForSelectedItems];
    [self.sureBtn setTitle:[NSString stringWithFormat:@"确认删除(%ld)",self.seleAry.count] forState:UIControlStateNormal];
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake((SCREEN_W - YNUM(60))/3, YNUM(210));
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.sectionInset = UIEdgeInsetsMake(0, YNUM(15), 0, YNUM(15));
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, GK_STATUSBAR_NAVBAR_HEIGHT, SCREEN_W, SCREEN_H - GK_STATUSBAR_NAVBAR_HEIGHT - YNUM(57) - GK_SAFEAREA_BTM) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.allowsMultipleSelection = YES;
        [_collectionView registerClass:[AY_BookShelEditCell class] forCellWithReuseIdentifier:@"AY_BookShelEditCell"];
    }
    return _collectionView;
}

- (void)setupNavView {
    self.gk_navigationBar.hidden = YES;
    UIView * bgV = [AYTool createViewWithFrame:CGRectMake(0, 0, GK_SCREEN_WIDTH, GK_STATUSBAR_NAVBAR_HEIGHT) andBg:[UIColor whiteColor] andRadius:0.0 andAddSubView:self.view];
    
    UILabel * titleLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"请选择书籍" andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(18)] andLines:2 andRadius:0 andAddSubView:bgV];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bgV.mas_bottom).offset(YNUM(-5));
        make.left.equalTo(bgV).offset(YNUM(12));
        make.height.offset(YNUM(24));
    }];
    
    UIButton * guanBiBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"关闭" andFont:YFont(14) andTitleColor:COLOR(@"#97A4B0") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(setCloseAction) andRadius:0 andAddSubView:bgV];
    [guanBiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLabel);
        make.right.equalTo(bgV).offset(YNUM(-20));
        make.height.mas_equalTo(YNUM(26));
        make.width.mas_equalTo(YNUM(32));
    }];
    self.quanXBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"全选" andFont:YFont(14) andTitleColor:COLOR(@"#97A4B0") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(setSelectAllAction) andRadius:0 andAddSubView:bgV];
    [self.quanXBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLabel);
        make.right.equalTo(guanBiBtn.mas_left).offset(YNUM(-18));
        make.height.mas_equalTo(YNUM(26));
        make.width.mas_equalTo(YNUM(45));
    }];
    
}

- (void)setupBootView {
    UIView *footerV = [[UIView alloc]initWithFrame:CGRectMake(0, GK_SCREEN_HEIGHT - YNUM(27) - GK_SAFEAREA_BTM, SCREEN_W, YNUM(27) + GK_SAFEAREA_BTM)];
    footerV.backgroundColor = [UIColor whiteColor];
    self.sureBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"确认删除" andFont:[UIFont boldSystemFontOfSize:16] andTitleColor:[UIColor whiteColor] andBg:THEME_COLOR andImage:nil andBgImage:nil andTarget:self andAction:@selector(handleSureAction) andRadius:YNUM(8) andAddSubView:footerV];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(footerV).offset(YNUM(-GK_SAFEAREA_BTM));
        make.left.equalTo(footerV).offset(YNUM(16));
        make.right.equalTo(footerV).offset(YNUM(-16));
        make.height.mas_offset(YNUM(45));
    }];
    [self.view addSubview:footerV];
}

- (void)handleSureAction {
    self.seleAry = [self.collectionView indexPathsForSelectedItems];
    [self updataData];
}

- (void)setCloseAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}

///全选
- (void)setSelectAllAction {
    self.isAllSel = !self.isAllSel;
    if(self.isAllSel) {
        [self.quanXBtn setTitle:@"全不选" forState:UIControlStateNormal];
    }else {
        [self.quanXBtn setTitle:@"全选" forState:UIControlStateNormal];
    }
    for (NSUInteger index = 0; index < self.dataSource.count; ++index) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
        if(self.isAllSel) {
            [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
        }else {
            [self.collectionView deselectItemAtIndexPath:indexPath animated:YES];
        }
    }
    self.seleAry = [self.collectionView indexPathsForSelectedItems];
    [self.sureBtn setTitle:[NSString stringWithFormat:@"确认删除(%ld)",self.seleAry.count] forState:UIControlStateNormal];

}

- (void)updataData {
    
    if(self.seleAry.count == 0) {
        ShowToast(@"请先选择一本~");
        return;
    }
    NSString * articleIds = @"";
    for (NSIndexPath * indx in self.seleAry) {
        if([articleIds isEqualToString:@""]) {
            articleIds = [NSString stringWithFormat:@"%@", [self.dataSource[indx.row] articleId]];
        }else {
            articleIds = [NSString stringWithFormat:@"%@,%@", articleIds, [self.dataSource[indx.row] articleId]];
        }
    }
    
    NSLog(@"%@", articleIds);
    [AYApiRequest getRemoveArticleShelfArticleIds:articleIds Success:^(AY_QiuBookModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            Weak_Self
            [self dismissViewControllerAnimated:YES completion:^{
                [weakSelf.shelfVc getData];
            }];
        }
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}

@end
